<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="dynamicFormRules">
                <el-form-item label="充值编号" v-show="isEditShow" prop="id">
                    <el-input v-model="formData.id" disabled placeholder="请输入" />
                </el-form-item>
                <el-form-item label="模板编号" v-show="!isEditShow" prop="t_id">
                    <el-select class="w-[280px]" v-model="formData.t_id">
                        <el-option
                            v-for="item in optionsData.articleCate"
                            :key="item.t_id"
                            :label="item.title"
                            :value="item.t_id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="充值类型" prop="type">
                    <el-select class="flex-1" v-model="formData.type" placeholder="请选择充值类型">
                        <el-option label="普通充值" :value="1" />
                        <el-option label="会员充值" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="标题" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入标题" />
                </el-form-item>
                <el-form-item label="充值金额" prop="price">
                    <el-input-number v-model="formData.price" :precision="2" :step="0.1" :min="0.01" :max="1000" />
                </el-form-item>
                <el-form-item label="币-赠送" v-if="formData.type == 1" prop="gift">
                    <el-input-number v-model="formData.gift" placeholder="请输入币-赠送" :min="0"/>
                </el-form-item>
                <el-form-item label="充值次数" prop="recharge_num">
                    <el-input-number v-model="formData.recharge_num" placeholder="请输入充值次数" :min="0" :max="100"/>
                </el-form-item>
                <el-form-item label="会员天数" v-if="formData.type == 2" prop="vip_days">
                    <el-input-number v-model="formData.vip_days" :min="0" :max="1000" />
                </el-form-item>
                <el-form-item label="角标" prop="sign">
                    <el-select class="w-[280px]" v-model="formData.sign">
                        <el-option
                            v-for="item in signOption.optionsData.articleCate"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input-number v-model="formData.sort" :min="0" :max="1000" placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="formData.status" placeholder="请选择状态">
                        <el-radio :label="1">显示</el-radio>
                        <el-radio :label="0">隐藏</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {
    rechargeGearEdit,
    rechargeGearAdd,
    rechargeGearDetail,
    rechargeGearSign
} from '@/api/client/recharge_gear'
import {  drechargeGearTypeAll } from '@/api/client/recharge_gear_type'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
import { useDictOptions } from '@/hooks/useDictOptions'
const { query } = useRoute()
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑充值档位' : '新增充值档位'
})

const isEditShow = computed(() => mode.value === 'edit')

const formData = reactive({
    id: 0,
    t_id: Number(query?.t_id ?? 0),
    type: 1,
    title: '',
    price: 0,
    gift: 0,
    vip_days: 0,
    sort: 0,
    recharge_num: 0,
    status: 1,
    sign: 0,
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    t_id: [
        {
            required: true,
            message: '请输入充值模板编号',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择充值类型',
            trigger: ['blur']
        }
    ],
    title: [
        {
            required: true,
            message: '请输入标题',
            trigger: ['blur']
        }
    ],
    price: [
        {
            required: true,
            message: '请输入充值金额',
            trigger: ['blur']
        }
    ],
    gift: [
        {
            required: false,
            message: '请输入币-赠送',
            trigger: ['blur']
        }
    ],
    vip_days: [
        {
            required: false,
            message: '请输入会员天数',
            trigger: ['blur']
        }
    ],
    sort: [
        {
            required: true,
            message: '请输入排序',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择状态',
            trigger: ['blur']
        }
    ],
}

// 修改为动态规则
const dynamicFormRules = computed(() => {
    const rules = { ...formRules }

    // 在添加模式下，account_id 不需要必填
    if (mode.value === 'add') {
        rules.id = [
            {
                required: false,
                message: '请输入主键',
                trigger: ['blur']
            }
        ]
    }
    // 在添加模式下，account_id 不需要必填
    if (mode.value !== 'add') {
        rules.t_id = [
            {
                required: false,
                message: '请输入选择模板',
                trigger: ['blur']
            }
        ]
    }

    return rules
})

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await rechargeGearEdit(data) : await rechargeGearAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            if (key == 'price'){
                formData[key] = Number(data[key])
            } else {
                formData[key] = data[key]
            }
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await rechargeGearDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

// 存储支付配置数据
const { optionsData } = useDictOptions<{
    articleCate: any[]
}>({
    articleCate: {
        api: drechargeGearTypeAll
    }
})
const signOption = useDictOptions<{
    articleCate: any[]
}>({
    articleCate: {
        api: rechargeGearSign
    }
})


console.log("signOption:",signOption.optionsData.articleCate)

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
